//
//  YeeNavHeaderScrollView.m
//  YWBaseFrameWork
//
//  Created by CoderYee on 2017/2/16.
//  Copyright © 2017年 NeiQuan. All rights reserved.
//

#import "YeeNavHeaderScrollView.h"
#define kDistanceBetweenItem 32
#define kExtraPadding 20
#define itemFont 15
@interface YeeNavHeaderScrollView ()
@property (nonatomic, strong) UIButton *btnSelect;
@property (nonatomic, assign) CGFloat maxWidth;
@property (nonatomic, strong) UIView *btnBackView;
@property (nonatomic, strong) NSMutableArray *btnLists; //导航条上的button集合
@end

@implementation YeeNavHeaderScrollView
-(NSMutableArray *)btnLists
{
    if (_btnLists == nil)
    {
        _btnLists = [NSMutableArray array];
    }
    return _btnLists;
}
-(void)setVisibleItemList:(NSMutableArray *)visibleItemList{
    
    _visibleItemList = visibleItemList;
    self.maxWidth = 20;
    self.backgroundColor = RGB(238.0, 238.0, 238.0);
    self.contentInset = UIEdgeInsetsMake(0, 0, 0, 50);
    self.showsHorizontalScrollIndicator = NO;
    for (int i =0; i<visibleItemList.count; i++)
    {
        [self makeItemWithTitle:visibleItemList[i]];
    }
    self.contentSize = CGSizeMake(self.maxWidth, self.frame.size.height);
}


-(void)makeItemWithTitle:(NSString *)title
{
    CGFloat itemW = [self calculateSizeWithFont:itemFont Text:title].size.width;
    UIButton *item = [[UIButton alloc] initWithFrame:CGRectMake(self.maxWidth, 0, itemW, self.frame.size.height)];
    item.titleLabel.font = [UIFont systemFontOfSize:itemFont];
    [item setTitle:title forState:UIControlStateNormal];
    [item setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [item setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    [item setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [item addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
    self.maxWidth += itemW+kDistanceBetweenItem;
    [self.btnLists addObject:item];
    [self addSubview:item];
    if (!self.btnSelect)//默认选中第一个
    {
        [item setTitleColor:kNavBarThemeColor forState:UIControlStateNormal];
        self.btnSelect = item;
    }
    self.contentSize = CGSizeMake(self.maxWidth, self.frame.size.height);
}
-(void)itemClick:(UIButton *)sender
{
    if (self.btnSelect != sender)
    {
        [self.btnSelect setTitleColor:[UIColor  blackColor] forState:UIControlStateNormal];
        [sender setTitleColor:kNavBarThemeColor forState:UIControlStateNormal];
        self.btnSelect = sender;
        
        if (self.listBarItemClickBlock) {
            self.listBarItemClickBlock(sender.titleLabel.text,[self findIndexOfListsWithTitle:sender.titleLabel.text]);
        }
    }
    // 设置标题滚动区域的偏移量
    CGFloat offsetX = sender.center.x - self.frame.size.width * 0.5;
    if (offsetX < 0)
    {
        offsetX = 0;
    }
    // 计算下最大的标题视图滚动区域
    CGFloat maxOffsetX = self.contentSize.width - self.frame.size.width;
    if (maxOffsetX < 0)
    {
        maxOffsetX = 0;
    }
    
    if (offsetX > maxOffsetX)
    {
        offsetX = maxOffsetX;
    }
    // 滚动区域
    [self setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

-(void)itemClickByScrollerWithIndex:(NSInteger)index
{
    UIButton *item = (UIButton *)self.btnLists[index];
    [self itemClick:item];
}
-(void)itemClickBySwipeTableViewWithIndex:(NSInteger)index
{
    UIButton *sender = (UIButton *)self.btnLists[index];
    
    if (self.btnSelect != sender)
    {
        [self.btnSelect setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [sender setTitleColor:kNavBarThemeColor forState:UIControlStateNormal];
        self.btnSelect = sender;
    }
    // 设置标题滚动区域的偏移量
    CGFloat offsetX = sender.center.x - self.frame.size.width * 0.5;
    if (offsetX < 0)
    {
        offsetX = 0;
    }
    // 计算下最大的标题视图滚动区域
    CGFloat maxOffsetX = self.contentSize.width - self.frame.size.width;
    if (maxOffsetX < 0)
    {
        maxOffsetX = 0;
    }
    
    if (offsetX > maxOffsetX)
    {
        offsetX = maxOffsetX;
    }
    // 滚动区域
    [self setContentOffset:CGPointMake(offsetX, 0) animated:YES];
   
}
-(void)switchPositionWithItemName:(NSString *)itemName index:(NSInteger)index
{
    UIButton *button = self.btnLists[[self findIndexOfListsWithTitle:itemName]];
    [self.visibleItemList removeObject:itemName];
    [self.btnLists removeObject:button];
    [self.visibleItemList insertObject:itemName atIndex:index];
    [self.btnLists insertObject:button atIndex:index];
    [self itemClick:self.btnSelect];
    [self resetFrame];
}

-(void)removeItemWithTitle:(NSString *)title
{
    NSInteger index = [self findIndexOfListsWithTitle:title];
    UIButton *select_button = self.btnLists[index];
    [self.btnLists[index] removeFromSuperview];
    [self.btnLists removeObject:select_button];
    [self.visibleItemList removeObject:title];
}

-(NSInteger)findIndexOfListsWithTitle:(NSString *)title{
    for (int i =0; i < self.visibleItemList.count; i++) {
        if ([title isEqualToString:self.visibleItemList[i]]) {
            return i;
        }
    }
    return 0;
}

-(void)resetFrame
{
    self.maxWidth = 20;
    for (int i = 0; i < self.visibleItemList.count; i++) {
        [UIView animateWithDuration:0.0001 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
            CGFloat itemW = [self calculateSizeWithFont:itemFont Text:self.visibleItemList[i]].size.width;
            [[self.btnLists objectAtIndex:i] setFrame:CGRectMake(self.maxWidth, 0, itemW, self.frame.size.height)];
            self.maxWidth += kDistanceBetweenItem + itemW;
        } completion:^(BOOL finished){
        }];
    }
    self.contentSize = CGSizeMake(self.maxWidth, self.frame.size.height);
}

-(CGRect)calculateSizeWithFont:(NSInteger)Font Text:(NSString *)Text{
    NSDictionary *attr = @{NSFontAttributeName : [UIFont systemFontOfSize:Font]};
    CGRect size = [Text boundingRectWithSize:CGSizeMake(MAXFLOAT, self.frame.size.height)
                                     options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
                                  attributes:attr
                                     context:nil];
    return size;
}
@end
